{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 导入google.colab包中的内容，实现硬盘挂在。\r\n",
    "from google.colab import drive\r\n",
    "drive.mount('/content/drive')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 更改到当前文件的目录，用于后续的工程执行\r\n",
    "import os\r\n",
    "os.chdir(\"/content/drive/MyDrive/federated_malware/Project/\")\r\n",
    "!pwd\r\n",
    "!ls"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 执行本地写好的测试脚本\r\n",
    "# !python fl_result_test.py"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# import os \r\n",
    "# if os.name == 'nt':\r\n",
    "#     os.chdir(\"C:\\\\federated_malware\\\\Project\")\r\n",
    "#     print (\"cwd\",os.getcwd())#获得当前目录\r\n",
    "#     print (\"工作目录\",os.path.abspath('.'))#获得当前工作目录\r\n",
    "#     print (\"工作目录\",os.path.abspath(os.curdir))#获得当前工作目录"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "cwd c:\\federated_malware\\Project\n",
      "工作目录 c:\\federated_malware\\Project\n",
      "工作目录 c:\\federated_malware\\Project\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "filelist = []\r\n",
    "filepath = './results/'\r\n",
    "# walk是为了遍历子目录的。一次就能多的当前目录下所有的files\r\n",
    "for root,dirs,files in os.walk(filepath):\r\n",
    "    filelist=files\r\n",
    "    break"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "for file in filelist:\r\n",
    "    print(file)\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "mdm_avg_fedavg.zip\n",
      "mdm_fn_fedavg.zip\n",
      "mdm_iid_fedavg.zip\n",
      "mdm_ld_fedavg.zip\n",
      "mdm_ln_fedavg.zip\n",
      "mdm_quantity_fedavg.zip\n",
      "['mdm_avg_fedavg.zip', 'mdm_fn_fedavg.zip', 'mdm_iid_fedavg.zip', 'mdm_ld_fedavg.zip', 'mdm_ln_fedavg.zip', 'mdm_quantity_fedavg.zip']\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "import torch \r\n",
    "import matplotlib.pyplot as plt\r\n",
    "for i,file in enumerate(filelist):\r\n",
    "    fig = plt.figure(i,figsize=(12,12),dpi=100)\r\n",
    "    j=1\r\n",
    "    data_dict = torch.load(filepath+file)\r\n",
    "    print(file)\r\n",
    "    if 'test_accuracy' in data_dict:\r\n",
    "        plt.subplot(3,2,j)\r\n",
    "        plt.plot(data_dict['test_accuracy'])\r\n",
    "        plt.title('test accuracy')\r\n",
    "        plt.xlabel('round')\r\n",
    "        plt.ylabel('acc')\r\n",
    "        plt.tight_layout()\r\n",
    "        j+=1\r\n",
    "    \r\n",
    "    if 'train_loss' in data_dict:\r\n",
    "        plt.subplot(3,2,j)        \r\n",
    "        plt.plot(data_dict['train_loss'])\r\n",
    "        plt.title('train loss')\r\n",
    "        plt.xlabel('round')\r\n",
    "        plt.ylabel('loss')\r\n",
    "        plt.tight_layout()\r\n",
    "        j+=1\r\n",
    "    \r\n",
    "    if 'train_accuracy' in data_dict:\r\n",
    "        plt.subplot(3,2,j)\r\n",
    "        plt.plot(data_dict['train_accuracy'])\r\n",
    "        plt.title('train accuracy')\r\n",
    "        plt.xlabel('round')\r\n",
    "        plt.ylabel('accuracy')\r\n",
    "        plt.tight_layout()\r\n",
    "        j+=1\r\n",
    "    \r\n",
    "    if 'information_increase_matrix' in data_dict:\r\n",
    "        plt.subplot(3,2,j)\r\n",
    "        plt.bar(range(len(data_dict['information_increase_matrix'])),data_dict['information_increase_matrix'])\r\n",
    "        plt.title('information_increase_matrix')\r\n",
    "        plt.xlabel('client number')\r\n",
    "        plt.ylabel('contribution')\r\n",
    "        plt.tight_layout()\r\n",
    "        j+=1\r\n",
    "    \r\n",
    "    if 'information_increase_list' in data_dict:\r\n",
    "        plt.subplot(3,2,j)\r\n",
    "        plt.plot(data_dict['information_increase_list'])\r\n",
    "        plt.title('information_increase_list')\r\n",
    "        plt.xlabel('round')\r\n",
    "        plt.ylabel('delta information')\r\n",
    "        plt.tight_layout()\r\n",
    "        j+=1\r\n",
    "\r\n",
    "    plt.show()"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "RuntimeError",
     "evalue": "version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at ..\\caffe2\\serialize\\inline_container.cc:132, please report a bug to PyTorch. Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2. Your PyTorch installation may be too old. (init at ..\\caffe2\\serialize\\inline_container.cc:132)\n(no backtrace available)",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-72eb5f01f64a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mfig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfigsize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m12\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m12\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdpi\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mj\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mdata_dict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mfile\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;34m'test_accuracy'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdata_dict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msubplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\envs\\pysyft\\lib\\site-packages\\torch\\serialization.py\u001b[0m in \u001b[0;36mload\u001b[1;34m(f, map_location, pickle_module, **pickle_load_args)\u001b[0m\n\u001b[0;32m    525\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0m_open_file_like\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mopened_file\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    526\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0m_is_zipfile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopened_file\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 527\u001b[1;33m             \u001b[1;32mwith\u001b[0m \u001b[0m_open_zipfile_reader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mopened_zipfile\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    528\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0m_load\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopened_zipfile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmap_location\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpickle_module\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mpickle_load_args\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    529\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0m_legacy_load\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopened_file\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmap_location\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpickle_module\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mpickle_load_args\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\envs\\pysyft\\lib\\site-packages\\torch\\serialization.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, name_or_buffer)\u001b[0m\n\u001b[0;32m    222\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0m_open_zipfile_reader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_opener\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    223\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname_or_buffer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 224\u001b[1;33m         \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_open_zipfile_reader\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPyTorchFileReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname_or_buffer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    225\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    226\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED at ..\\caffe2\\serialize\\inline_container.cc:132, please report a bug to PyTorch. Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2. Your PyTorch installation may be too old. (init at ..\\caffe2\\serialize\\inline_container.cc:132)\n(no backtrace available)"
     ]
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": [
       "<Figure size 1200x1200 with 0 Axes>"
      ]
     },
     "metadata": {}
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.8.8",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('pysyft': conda)"
  },
  "interpreter": {
   "hash": "57f55249afac9e3bb90b27c0916a1d44f0a08c86299e4ac4c83ac98b0a805cf4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}